import wx
from Pointf import Pointf
from UI.TileLayer import TileLayer
from UI.MapControl import MapControl
from Config.ConfigOsm import ConfigOsm
from UI.MapTransform import MapTransform
from UI.MapTransformHelpers import MapTransformHelpers

import threading

ID_ABOUT=101
ID_EXIT=110

class BruTile(wx.Panel):

    max_x = 835
    max_y = 786
    
    window_width = None
    window_height = None
    window_offset = 0
    
    map_control = MapControl()
    
    def __init__(self,parent,id, width, height):
        wx.Panel.__init__(self,parent,id,wx.Point(0,0), (width,height), 0,"")
        #wx.Panel.__init__(self,parent,id,wx.Point(0,0), (835,768), 0,"")
        threading.current_thread().setName("BruTile")
        self.SetFocus()
        wx.EVT_SIZE(self, self.OnResize)
        #print(width, height)
        self.window_width = width
        self.window_height = height
        self.init_map()
        self.bind_mouse_events()
        self.map_control.refresh()
    
    def resize(self, frame_size):
        size = [500,1000]
        self.SetSize(frame_size)
        #self.window_width = frame_size[0] - self.window_offset
        self.window_width = frame_size[0]
        self.window_height = frame_size[1]
        
        self.map_control.map_transform.width = self.window_width
        self.map_control.map_transform.height = self.window_height
        self.Refresh(False,self.GetClientRect())
        #print(frame_size)
        
    def bind_mouse_events(self):
        #self.Bind(wx.EVT_LEFT_DOWN, self.map_control.mouse_handler.leftMouseDown)
        self.Bind(wx.EVT_LEFT_DOWN, self.LeftMouseClick)
        self.Bind(wx.EVT_RIGHT_DOWN, self.map_control.mouse_handler.rightMouseDown)
        self.Bind(wx.EVT_MOTION, self.map_control.mouse_handler.mouseMove)
        self.Bind(wx.EVT_MOUSEWHEEL, self.map_control.mouse_handler.onScrollwheel)
        #self.Bind(wx.EVT_MOUSEWHEEL, self.OnScrollWheel)
        self.Bind(wx.EVT_RIGHT_UP, self.map_control.mouse_handler.rightMouseUp)
        #self.Bind(wx.EVT_RIGHT_UP, self.map_control.zoomToBoundingBox)
        self.Bind(wx.EVT_PAINT, self.on_paint)
    
    def init_map(self):
        tile_schema = ConfigOsm.getSchema()
        self.map_control.super_refresh = self.refresh
        self.map_control.tile_layer = TileLayer(tile_schema, self.data_changed)
        #self.map_control.tile_layer.tile_fetcher.nr = 98
        #self.map_control.map_transform = MapTransform(Pointf(4.887543,52.337856), 156543.033900000, self.window_width, self.window_height)
        self.map_control.map_transform = MapTransform(Pointf(0,0), 156543.033900000, self.window_width, self.window_height)
    
    def LeftMouseClick(self, event):
        self.SetFocus()
        self.map_control.mouse_handler.leftMouseDown(event)
    
    def OnResize(self, event):
        size = [event.Size.x,event.Size.y]
        self.SetSize(size)

        self.window_width = size[0]
        self.window_height = size[1]
        
        new_mouse_pos = Pointf((self.window_width),(self.window_height))
        
        self.Refresh()
    
    def refresh(self, extent):
        self.Refresh(False,self.GetClientRect())
    
    def on_paint(self, event):
        dc = wx.PaintDC(self)
        self.map_control.draw(dc, self.window_width, self.window_height)

    def _print(self):
        print("BruTile: Quit")
        
    def shut_down(self):
        self.map_control.tile_layer.tile_fetcher.QuitThread = self._print
        self.map_control.tile_layer.tile_fetcher.stopFetchingThread()
    
    def data_changed(self, tile_info):
        self.Refresh(False,self.GetClientRect())